package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.ProjectReceiptBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.ProjectReceiptMapper;
import com.koron.css2.newProjectCenter.service.PjInfoService;
import com.koron.css2.newProjectCenter.vo.PjInfoListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.CommonUtils;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.HashMap;
import java.util.List;

/**
 * 工程款冲单接口
 * @author xusheng
 *
 */
public class PjRushRed  implements ServerInterface {
	
	@SuppressWarnings("unchecked")
	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

		try {
			PjInfoListVO vo = JsonUtils.objectToPojo(req.getData(), PjInfoListVO.class);
			List<HashMap<String, String>> list = vo.getIds();
			//判断是否可以冲单
			String checkResult = check(list);
			if(!"OK".equals(checkResult)) {
				return MessageBean.create(Constant.MESSAGE_INT_FAIL, checkResult, null);
			}
			//多条一起冲单
			for (HashMap<String, String> map : list) {
				//查询工程
				PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
				PjInfoBean pjInfoBean = pjInfoMapper.selectById(map.get("id"));
				if (pjInfoBean == null) {
					return MessageBean.create(Constant.MESSAGE_INT_FAIL, "用水工程单据不存在。", null);
				}
				Double rushRedMoney = pjInfoMapper.rushRedMoneyQuery(map.get("receiptId"));
				pjInfoBean.setActuallyReceived(-rushRedMoney);
				//增加一条反向单据
				addProjectReceipt(factory, userInfo, pjInfoBean,"2");
				//累计金额减去当前金额
				pjInfoBean.setAccruedMoney((pjInfoBean.getAccruedMoney()==null?0.0:pjInfoBean.getAccruedMoney()) - rushRedMoney);
				pjInfoMapper.saveAccruedMoney(pjInfoBean);
			}
			return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", null);
		} catch (Exception e) {
			factory.close(false);
			logger.error("工程款调整保存失败"+e.getMessage(), e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "保存失败", null);
		}
	}
	
	private String check(List<HashMap<String, String>> list) {
		for (HashMap<String, String> map : list) {
			if(!map.get("chargeDate").toString().equals(CommonUtils.getCurrentDate())) {
				return "只允许冲单当日单据";
			}
		}
		return "OK";
	}

	public void addProjectReceipt(SessionFactory factory, UserInfoBean userInfo,PjInfoBean pjInfoBean, String chargeType) {
		
		//组装工程收款单
		ProjectReceiptBean projectReceiptBean = new ProjectReceiptBean();
		projectReceiptBean.setId(Tools.getObjectId());
		projectReceiptBean.setBillId(pjInfoBean.getId());
		projectReceiptBean.setBillNo(pjInfoBean.getBillNo());
		projectReceiptBean.setBillDate(CommonUtils.getCurrentDate());
		projectReceiptBean.setBillType(PjInfoService.PROJECT_INFO_NEW_RECEIPT);
		projectReceiptBean.setReceivableMoney(pjInfoBean.getActuallyReceived());//应收
		projectReceiptBean.setProceedsMoney(pjInfoBean.getActuallyReceived());//实收
		projectReceiptBean.setDepositMoney(0.0d);//押金
//		double accmoney = (pjInfoBean.getBudgetMoney()==null?0.0:pjInfoBean.getBudgetMoney())+(pjInfoBean.getDepositMoney()==null?0.0:pjInfoBean.getDepositMoney());
		projectReceiptBean.setTotalMoney(pjInfoBean.getActuallyReceived());
		projectReceiptBean.setChargeFlag(1);
		projectReceiptBean.setReturnFlag(1);
		projectReceiptBean.setComments("工程款调整单据编号：" + pjInfoBean.getBillNo());
		projectReceiptBean.setTenantId(userInfo.getCurWaterCode());
		projectReceiptBean.setCreateAccount(userInfo.getUserInfo().getAcount());
		projectReceiptBean.setCreateName(userInfo.getUserInfo().getName());
		projectReceiptBean.setChargeType(chargeType);
		
		//新增工程收款单
		ProjectReceiptMapper projectReceiptMapper = factory.getMapper(ProjectReceiptMapper.class);
		projectReceiptMapper.addNew(projectReceiptBean);
	}
	
}
